package com.opensource.svgaplayer.drawer;

import com.opensource.svgaplayer.SVGAVideoEntity;
import com.opensource.svgaplayer.entities.SVGAVideoSpriteEntity;
import com.opensource.svgaplayer.entities.SVGAVideoSpriteFrameEntity;
import com.opensource.svgaplayer.utils.Pools;
import com.opensource.svgaplayer.utils.SVGAScaleInfo;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import java.util.ArrayList;
import java.util.List;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class SGVADrawer {
    private SVGAVideoEntity videoItem;
    protected SVGAScaleInfo scaleInfo;
    private Pools.SimplePool<SVGADrawerSprite> spritePool;

    /**
     * getVideoItem
     *
     * @return SVGAVideoEntity
     */
    public SVGAVideoEntity getVideoItem() {
        return videoItem;
    }

    /**
     * getScaleInfo
     *
     * @return SVGAVideoEntity
     */
    public SVGAScaleInfo getScaleInfo() {
        return scaleInfo;
    }

    public SGVADrawer(SVGAVideoEntity videoItem) {
        this.videoItem = videoItem;
        this.scaleInfo = new SVGAScaleInfo();
        int maxSize = Math.max(1, videoItem.spriteList.size());
        this.spritePool = new Pools.SimplePool(maxSize);
    }

    /**
     * getSpritePool
     *
     * @return Pools.SimplePool
     */
    public Pools.SimplePool getSpritePool() {
        return spritePool;
    }

    /**
     * requestFrameSprites
     *
     * @param frameIndex frameIndex
     * @return List<SVGADrawerSprite>
     */
    public List<SVGADrawerSprite> requestFrameSprites(int frameIndex) {
        List<SVGAVideoSpriteEntity> spriteList = videoItem.spriteList;
        List<SVGADrawerSprite> tempLsit = new ArrayList<>();
        for (int i = 0; i < spriteList.size(); i++) {
            SVGAVideoSpriteEntity entity = spriteList.get(i);
            if (frameIndex >= 0 && frameIndex < entity.getFrames().size()) {
                String imageKey = entity.getImageKey();
                if (!imageKey.endsWith(".matte") && entity.getFrames().get(frameIndex).getAlpha() <= 0.0) {
                    tempLsit.add(null);
                }
                SVGADrawerSprite acquire = spritePool.acquire();
                if (acquire == null) {
                    acquire = new SVGADrawerSprite();
                }
                acquire.setMatteKey(entity.getMatteKey());
                acquire.setImageKey(entity.getImageKey());
                acquire.setFrameEntity(entity.getFrames().get(frameIndex));
                tempLsit.add(acquire);
            }
            tempLsit.add(null);
        }
        for (int i = 0; i < tempLsit.size(); i++) {
            SVGADrawerSprite it = tempLsit.get(i);
            if (it == null) {
                tempLsit.remove(i);
                i--;
            }
        }
        return tempLsit;
    }

    /**
     * releaseFrameSprites
     *
     * @param sprites List<SVGADrawerSprite>
     */
    protected void releaseFrameSprites(List<SVGADrawerSprite> sprites) {
        for (SVGADrawerSprite it : sprites) {
            spritePool.release(it);
        }
    }

    /**
     * drawFrame
     *
     * @param canvas     canvas
     * @param width      width
     * @param height     height
     * @param frameIndex frameIndex
     * @param scaleType  scaleType
     */
    public void drawFrame(Canvas canvas, int width, int height, int frameIndex, Image.ScaleMode scaleType) {
        scaleInfo.performScaleType(width, height, (float) videoItem.getVideoSize().getWidth(), (float) videoItem.getVideoSize().getHeight(), scaleType);
    }

    static class SVGADrawerSprite {
        private String matteKey;
        private String imageKey;
        private SVGAVideoSpriteFrameEntity frameEntity;

        /**
         * setMatteKey
         *
         * @param matteKey matteKey
         */
        public void setMatteKey(String matteKey) {
            this.matteKey = matteKey;
        }

        /**
         * setImageKey
         *
         * @param imageKey imageKey
         */
        public void setImageKey(String imageKey) {
            this.imageKey = imageKey;
        }

        /**
         * setFrameEntity
         *
         * @param frameEntity frameEntity
         */
        public void setFrameEntity(SVGAVideoSpriteFrameEntity frameEntity) {
            if (frameEntity == null) {
                throw new IllegalArgumentException("frameEntity not is null");
            }
            this.frameEntity = frameEntity;
        }

        /**
         * SVGADrawerSprite
         */
        public SVGADrawerSprite() {

        }

        /**
         * getMatteKey
         *
         * @return String
         */
        public String getMatteKey() {
            return matteKey;
        }

        /**
         * getImageKey
         *
         * @return String
         */
        public String getImageKey() {
            return imageKey;
        }

        /**
         * getFrameEntity
         *
         * @return SVGAVideoSpriteFrameEntity
         */
        public SVGAVideoSpriteFrameEntity getFrameEntity() {
            return frameEntity;
        }
    }
}




